package com.rpoonolly.simplegame.mapio.jsoncoders;

import java.io.IOException;

import org.json.simple.JSONObject;

import com.rpoonolly.simplegame.worldGrid.GridCoordinate;

public class GridCoordinateJSONCoder extends JSONCoder<GridCoordinate> {
	private static GridCoordinateJSONCoder singletonInstance = null;
	
	private GridCoordinateJSONCoder() {}

	@SuppressWarnings("unchecked")
	@Override
	public JSONObject encode(GridCoordinate obj) throws IOException {
		JSONObject jsonCoord = new JSONObject();
		jsonCoord.put("x", obj.getX());
		jsonCoord.put("y", obj.getY());
		
		return jsonCoord;
	}

	@Override
	public GridCoordinate decode(JSONObject jsonCoord) throws IOException {
		int x = (Integer) jsonCoord.get("x");
		int y = (Integer) jsonCoord.get("y");
		
		return new GridCoordinate(x, y);
	}

	public static GridCoordinateJSONCoder getInstance() {
		if (singletonInstance == null)
			singletonInstance = new GridCoordinateJSONCoder();
		
		return singletonInstance;
	}
}
